Imports System.IO
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Xml
Imports ICSharpCode.AvalonEdit
Imports ICSharpCode.AvalonEdit.CodeCompletion
Imports ICSharpCode.AvalonEdit.Document
Imports ICSharpCode.AvalonEdit.Editing
Imports ICSharpCode.AvalonEdit.Highlighting

Public Class Form1
  Private ReadOnly codedit As New TextEditor
  Private CompletionWindow As CompletionWindow
  Public Sub New()
    InitializeComponent()
    AddHandler codedit.TextArea.TextEntering, New TextCompositionEventHandler(AddressOf TextAreaOnTextEntering)
    AddHandler codedit.TextArea.TextEntered, New TextCompositionEventHandler(AddressOf TextAreaOnTextEntered)
  End Sub

  Private Sub TextAreaOnTextEntering(sender As Object, e As TextCompositionEventArgs)
    If e.Text.Length > 0 AndAlso CompletionWindow IsNot Nothing Then
      If Not Char.IsLetterOrDigit(e.Text(0)) Then
        CompletionWindow.CompletionList.RequestInsertion(e)
      End If
    End If
  End Sub


  Private Sub TextAreaOnTextEntered(sender As Object, e As TextCompositionEventArgs)
    If e.Text = "." Then
      CompletionWindow = New CompletionWindow(codedit.TextArea)
      Dim data As IList(Of ICompletionData) = CompletionWindow.CompletionList.CompletionData
      data.Add(New MyCompletionData("Item1"))
      data.Add(New MyCompletionData("Item2"))
      data.Add(New MyCompletionData("Item3"))
      CompletionWindow.Show()
      'AddHandler CompletionWindow.Closed, New EventHandler(AddressOf m_completionWindow_Closed)
    End If
  End Sub




  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ''加载自定义高亮文件
    Dim customHighlighting As IHighlightingDefinition
    Dim fs As New FileStream(Application.StartupPath & "\CustomHighlighting.xshd", FileMode.Open, FileAccess.Read)
    Dim buffer As Byte() = New Byte(fs.Length - 1) {}
    fs.Read(buffer, 0, buffer.Length)
    Using s As Stream = New MemoryStream(buffer)
      Using reader As XmlReader = New XmlTextReader(s)
        customHighlighting = Xshd.HighlightingLoader.Load(reader, HighlightingManager.Instance)
      End Using
    End Using
    ''------------------------
    ''创建代码控件
    With codedit
      .ShowLineNumbers = True
      .Padding = New Windows.Thickness(20)
      .FontFamily = New FontFamily("Console")
      .FontSize = 14
      '.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("VB")
      .SyntaxHighlighting = customHighlighting
      .TabIndex = 4
      .Encoding = System.Text.Encoding.UTF8
      .Background = New SolidColorBrush(Color.FromRgb(37, 37, 38)) '背景色
      .Foreground = New SolidColorBrush(Color.FromRgb(220, 220, 220)) '前景色
    End With
    ElementHost1.Child = codedit
    ''---------
    '加载代码
    codedit.Text = "
Imports System.IO
Imports System.Xml
Imports ICSharpCode.AvalonEdit
Imports ICSharpCode.AvalonEdit.Highlighting

Public Class Form1
  Private ReadOnly codedit As New TextEditor
  Public Sub New()
    InitializeComponent()
    ''加载自定义高亮文件
    Dim customHighlighting As IHighlightingDefinition
    Dim fs As New FileStream(Application.StartupPath & ""\CustomHighlighting.xshd"", FileMode.Open, FileAccess.Read)
    Dim buffer As Byte() = New Byte(fs.Length - 1) {}
    fs.Read(buffer, 0, buffer.Length)
    Using s As Stream = New MemoryStream(buffer)
      Using reader As XmlReader = New XmlTextReader(s)
        customHighlighting = Xshd.HighlightingLoader.Load(reader, HighlightingManager.Instance)
      End Using
    End Using
    ''------------------------
    ''创建代码控件
    With codedit
      .ShowLineNumbers = True
      .Padding = New Windows.Thickness(20)
      .FontFamily = New Windows.Media.FontFamily(""Console"")
      .FontSize = 14
      '.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition(""VB"")
      .SyntaxHighlighting = customHighlighting
      .TabIndex = 4
      .Encoding = System.Text.Encoding.UTF8
      .Background = New Windows.Media.SolidColorBrush(Windows.Media.Color.FromRgb(37, 37, 38)) '背景色
      .Foreground = New Windows.Media.SolidColorBrush(Windows.Media.Color.FromRgb(220, 220, 220)) '前景色
    End With
    ElementHost1.Child = codedit
  End Sub


End Class

                  "
  End Sub
End Class

Public Class MyCompletionData
  Implements ICompletionData
  Public Sub New(text As String)
    Me.Text = text
  End Sub

  Public ReadOnly Property Image() As System.Windows.Media.ImageSource
    Get
      Return Nothing
    End Get
  End Property

  Public Property Text() As String


  Public ReadOnly Property Content() As Object
    Get
      Return Me.Text
    End Get
  End Property

  Public ReadOnly Property Description() As Object
    Get
      Return "Description for " + Me.Text
    End Get
  End Property

  Public ReadOnly Property Priority As Double Implements ICompletionData.Priority
    Get
      Throw New NotImplementedException()
    End Get
  End Property

  Private ReadOnly Property ICompletionData_Image As ImageSource Implements ICompletionData.Image
    Get
      Throw New NotImplementedException()
    End Get
  End Property

  Private ReadOnly Property ICompletionData_Text As String Implements ICompletionData.Text
    Get
      Throw New NotImplementedException()
    End Get
  End Property

  Private ReadOnly Property ICompletionData_Content As Object Implements ICompletionData.Content
    Get
      Throw New NotImplementedException()
    End Get
  End Property

  Private ReadOnly Property ICompletionData_Description As Object Implements ICompletionData.Description
    Get
      Throw New NotImplementedException()
    End Get
  End Property

  Public Sub Complete(textArea As TextArea, completionSegment As ISegment, insertionRequestEventArgs As EventArgs)
    textArea.Document.Replace(completionSegment, Me.Text)
  End Sub

  Private Sub ICompletionData_Complete(textArea As TextArea, completionSegment As ISegment, insertionRequestEventArgs As EventArgs) Implements ICompletionData.Complete
    Throw New NotImplementedException()
  End Sub
End Class
